<script lang="ts" setup>
import type { EdubaseSchoolTeachingUnitApi } from '#/api/edu/base/schoolTeachingUnit';

import { computed, ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import {
  createSchoolTeachingUnit,
  getSchoolTeachingUnit,
  updateSchoolTeachingUnit,
} from '#/api/edu/base/schoolTeachingUnit';
import { $t } from '#/locales';

import { useFormSchema } from '../data';

const emit = defineEmits(['success']);
const formData = ref<EdubaseSchoolTeachingUnitApi.SchoolTeachingUnit>();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['教学单位'])
    : $t('ui.actionTitle.create', ['教学单位']);
});

const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 120,
  },
  wrapperClass: 'grid-cols-1',
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
});

const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data =
      (await formApi.getValues()) as EdubaseSchoolTeachingUnitApi.SchoolTeachingUnit;
    try {
      await (formData.value?.id
        ? updateSchoolTeachingUnit(data)
        : createSchoolTeachingUnit(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    let data =
      modalApi.getData<EdubaseSchoolTeachingUnitApi.SchoolTeachingUnit>();
    if (!data || !data.id) {
      formData.value = data;
      console.log(data);
      await formApi.setValues(data);
      return;
    }
    console.log(data);
    let orgInfoData = {
      showSchool: false,
      showCampus: false,
      showCollege: false,
      showDepartment: false,
      showFaculty: false,
      showGroup: false,
      isSchool: false,
    };
    if (data.isSchool) {
      if (data.showCampus) {
        orgInfoData.campusDictList = data.campusDictList || [];
      }
      if (data.showCollege) {
        orgInfoData.collegeDictList = data.collegeDictList || [];
      }
      if (data.showDepartment) {
        orgInfoData.departmentDictList = data.departmentDictList || [];
      }
      if (data.showFaculty) {
        orgInfoData.facultyDictList = data.facultyDictList || [];
      }
      if (data.showGroup) {
        orgInfoData.groupDictList = data.groupDictList || [];
      }
      orgInfoData.isSchool = data.isSchool;
      orgInfoData.showCampus = data.showCampus;
      orgInfoData.showCollege = data.showCollege;
      orgInfoData.showDepartment = data.showDepartment;
      orgInfoData.showFaculty = data.showFaculty;
      orgInfoData.showGroup = data.showGroup;
      orgInfoData.showSchool = data.showSchool;
      orgInfoData.distabledOrgType = data.distabledOrgType;
    } else {
      orgInfoData = {
        showSchool: false,
        showCampus: false,
        showCollege: false,
        showDepartment: false,
        showFaculty: false,
        showGroup: false,
        isSchool: false,
      };
    }
    if (data.id) {
      modalApi.lock();
      try {
        data = await getSchoolTeachingUnit(data.id, false);
      } finally {
        modalApi.unlock();
      }
    }
    // 设置到 values
    formData.value = { ...data, ...orgInfoData };
    await formApi.setValues(formData.value);
  },
});
</script>

<template>
  <Modal :title="getTitle" class="w-[60%]">
    <Form class="mx-4" />
  </Modal>
</template>
